import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:short_drama/models/Invite_model.dart';
import 'package:short_drama/models/user_model.dart';
import 'package:short_drama/pages/login/login.dart';
import 'package:short_drama/pages/mine/invite.dart';
import 'package:short_drama/pages/mine/mine_%20earnings.dart';
import 'package:short_drama/pages/mine/profit.dart';
import 'package:short_drama/pages/mine/setting.dart';
import 'package:short_drama/pages/mine/user.dart';
import 'package:short_drama/pages/mine/verify.dart';
import 'package:short_drama/pages/mine/wallet.dart';
import 'package:short_drama/utils/colors.dart';
import 'package:short_drama/pages/widgets/dialog.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class PersonalCenterPage extends StatefulWidget {
  const PersonalCenterPage({Key? key}) : super(key: key);

  State<PersonalCenterPage> createState() => _PersonalCenterPageState();
}

class _PersonalCenterPageState extends State<PersonalCenterPage> {
  String serviceUrl = '';
  String headUrl = '';
  UserModel viewModel = UserModel(
    '',
    0,
    '',
    '',
    '',
    '',
    '',
    '',
    '',
    0,
    '',
    '',
    0,
  );

  @override
  void initState() {
    super.initState();
    _getServiceData();
    _getUserInfoData();
  }

  _getUserInfoData() async {
    try {
      final model = await UserViewModel().fetchUserInfoData();
      print(viewModel);
      setState(() {
        viewModel = model;
        headUrl = viewModel.headimg ?? '';
      });
    } catch (e) {
      print('获取数据失败_getServiceData');
    }
  }

  _getServiceData() async {
    try {
      final serviceImage = await ServiceViewModel().fetchProfitData();
      serviceUrl = serviceImage;
    } catch (e) {
      print('获取数据失败_getServiceData');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ClientColors.ff0a0f12,
      appBar: AppBar(
        backgroundColor: ClientColors.ff0a0f12,
        title: Text(
          '个人中心',
          style: TextStyle(color: Colors.white, fontSize: 18),
        ),
        elevation: 0,
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          //用户信息
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: Row(
              children: [
                GestureDetector(
                  onTap: () async {
                    var result = await Get.to(
                      () => UserPage(),
                      arguments: {'invite': viewModel.invite_member_no},
                    );
                    if (result != null) {
                      final data = result['headUrl'] ?? '';
                      if (data != null) {
                        setState(() {
                          headUrl = data;
                        });
                      }
                    }
                  },
                  child:
                      (viewModel.headimg.length > 0)
                          ? Container(
                            height: 80,
                            width: 80,
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(40),
                              image: DecorationImage(
                                image: NetworkImage(headUrl),
                                fit: BoxFit.cover,
                              ),
                            ),
                            child: SizedBox(),
                          )
                          : CircleAvatar(
                            radius: 40,
                            backgroundImage: AssetImage(
                              'assets/images/icon_user_head.png',
                            ),
                          ),
                ),
                SizedBox(width: 16),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      viewModel.nickname,
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 20,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    SizedBox(height: 4),
                    Text(
                      'ID: ${viewModel.member_no}',
                      style: TextStyle(color: ClientColors.aaffffff),
                    ),
                  ],
                ),
              ],
            ),
          ),
          //功能卡片
          Container(
            margin: EdgeInsets.only(top: 8, left: 11, right: 11, bottom: 12),
            child: Row(
              spacing: 12,
              children: [
                Expanded(
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 8),
                    child: GestureDetector(
                      onTap: () {
                        Get.to(() => WalletPage());
                      },
                      child: Container(
                        height: 80,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(8),
                          image: DecorationImage(
                            image: AssetImage(
                              'assets/images/icon_user_wallet.png',
                            ),
                            fit: BoxFit.cover,
                          ),
                        ),
                        child: Stack(
                          children: [
                            Positioned(
                              top: 20,
                              left: 20,
                              child: Text(
                                '我的钱包',
                                style: TextStyle(
                                  fontSize: 18,
                                  fontWeight: FontWeight.bold,
                                  color: ClientColors.ffa74d4a,
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
                Expanded(
                  child: GestureDetector(
                    onTap: () {
                      // EasyLoading.showToast('功能开发中');
                      Get.to(() => MineEarningsPage());
                    },
                    child: Container(
                      height: 80,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(10),
                        image: DecorationImage(
                          image: AssetImage('assets/images/icon_user_shop.png'),
                          fit: BoxFit.cover,
                        ),
                      ),
                      child: Stack(
                        children: [
                          Positioned(
                            top: 20,
                            left: 20,
                            child: Text(
                              '商户中心',
                              style: TextStyle(
                                fontSize: 18,
                                fontWeight: FontWeight.bold,
                                color: ClientColors.ff4359aa,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
          // 功能列表
          Expanded(
            child: Container(
              margin: EdgeInsets.fromLTRB(16, 20, 16, 20),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                color: ClientColors.odffffff,
              ),
              alignment: Alignment.center,
              child: ListView(
                children: [
                  ListTile(
                    leading: Image.asset('assets/images/icon_user_profit.png'),
                    title: Text('认购记录', style: TextStyle(color: Colors.white)),
                    trailing: Image.asset(
                      'assets/images/icon_user_arrow.png',
                      width: 6,
                      height: 12,
                    ),
                    onTap: () {
                      Get.to(() => ProfitPage());
                    },
                  ),
                  ListTile(
                    leading: Image.asset('assets/images/icon_user_me.png'),
                    title: Text('个人信息', style: TextStyle(color: Colors.white)),
                    trailing: Image.asset(
                      'assets/images/icon_user_arrow.png',
                      width: 6,
                      height: 12,
                    ),
                    onTap: () {
                      Get.to(
                        () => UserPage(),
                        arguments: {'invite': viewModel.invite_member_no},
                      );
                    },
                  ),
                  ListTile(
                    leading: Image.asset('assets/images/icon_user_invite.png'),
                    title: Text('邀请好友', style: TextStyle(color: Colors.white)),
                    trailing: Image.asset(
                      'assets/images/icon_user_arrow.png',
                      width: 6,
                      height: 12,
                    ),
                    onTap: () {
                      Get.to(() => InvitePage());
                    },
                  ),
                  ListTile(
                    leading: Image.asset('assets/images/icon_user_service.png'),
                    title: Text('我的客服', style: TextStyle(color: Colors.white)),
                    trailing: Image.asset(
                      'assets/images/icon_user_arrow.png',
                      width: 6,
                      height: 12,
                    ),
                    onTap: () async {
                      await Get.dialog(
                        CustomServiceDialog(imageUrl: serviceUrl),
                        barrierDismissible: true,
                      );
                    },
                  ),
                  ListTile(
                    leading: Image.asset('assets/images/icon_user_certify.png'),
                    title: Text('实名认证', style: TextStyle(color: Colors.white)),
                    trailing: Image.asset(
                      'assets/images/icon_user_arrow.png',
                      width: 6,
                      height: 12,
                    ),
                    onTap: () {
                      Get.to(() => RealNameAuthentication());
                    },
                  ),
                  // ListTile(
                  //   leading: Image.asset('assets/images/icon_user_add.png'),
                  //   title: Text(
                  //     '加入商户',
                  //     style: TextStyle(color: Colors.white),
                  //   ),
                  //   trailing: Image.asset(
                  //     'assets/images/icon_user_arrow.png',
                  //     width: 6,
                  //     height: 12,
                  //   ),
                  // ),
                  ListTile(
                    leading: Image.asset('assets/images/icon_user_set.png'),
                    title: Text('设置', style: TextStyle(color: Colors.white)),
                    trailing: Image.asset(
                      'assets/images/icon_user_arrow.png',
                      width: 6,
                      height: 12,
                    ),
                    onTap: () {
                      Get.to(() => SettingPage());
                    },
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
